// pages/index/tmp/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: 0,
    id: 0,
    title: '-', //物品名
    describe: '-',
    code: '', //二维码码值
    show_code: '', //二维码显示值
    prefixUrl: '',
    thumb: '',
    visit_total: 0,
    lasttime: '', //最近时间
    attrList: [], //属性集合
    reply_total: 0,
    replyList: [], //回复集合
    follow_total: 0,
    followList: [], //关注集合
    // 以下是弹窗
    pop1: 1, //弹窗物品名：0出现1关闭
    pop2: 1, //弹窗属性
    attr_key: '', attr_val: '', attr_id: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
        wx.showToast({
          title: '没有登录', icon: 'none',
        }); return
    }
    this.setData({
        uid: userInfo.uid,
        code: options.code,
    });
    this.getOne(options.code);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 获取数据
  getOne: function (v) {
    var that = this;
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/tmp/one',
      data: {code: v},
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        that.setData({
          id: res.data.id,
          title: res.data.title, 
          describe: res.data.describe,
          prefixUrl: res.data.prefixUrl,
          thumb: res.data.thumb,
          code: res.data.code, 
          show_code: res.data.show_code,
          visit_total: res.data.visit_total,
          lasttime: res.data.lasttime, 
          attrList: res.data.attrList,
          reply_total: res.data.reply_total,
          replyList: res.data.replyList,
          follow_total: res.data.follow_total,
          followList: res.data.followList,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

//   // 返回
//   toBack: function (e) {
//     console.log(e);
//     wx.navigateBack({
//       delta: 1
//     }); return
//   },

  // 选择图片
  chooseImage: function (e) {
    // wx.showLoading({
    //     title: '正在处理图片...',
    // });
    wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: res => {
            // var base64 = 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64")
            // 获取文件路径
            const filePath = res.tempFilePaths[0];
            console.log(filePath);
            // 读取文件
            wx.getFileSystemManager().readFile({
                filePath: filePath,
                encoding: 'base64',
                success: res => {
                    // 将文件内容转换为base64字符串
                    const base64Data = 'data:image/png;base64,' + res.data;
                    // 可以在这里使用base64字符串
                    console.log(base64Data);
                    // wx.showToast({
                    //   title: base64Data, icon: 'none'
                    // }); return
                    this.upload(base64Data);
                },
                fail: err => {
                    console.error('读取文件失败', err);
                    wx.showToast({
                      title: '读取文件失败', icon: 'none'
                    }); return
                }
            });
        },
        fail: err => {
            console.error('选择图片失败', err);
            wx.showToast({
              title: '选择图片失败', icon: 'none'
            }); return
        }
    });
  },

  // 上传图片
  upload: function (e) {
    console.log(e);
    var that = this;
    wx.request({
        method: 'POST',
        url: app.globalData.domainXcx + '/tmp/upload/thumb',
        data: {id: this.data.id, base64: e},
        timeout: 10000, //接口超时10S
        success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
                wx.showToast({
                    title: res.msg, icon: 'none'
                }); return
            }
            wx.showToast({
              title: '上传成功', icon: 'none'
            });
            that.viewReload();
        },
        fail: function () {
            console.log('error');
        }
    });
  },

  // 弹窗
  popShow: function (e) {
    console.log(e);
    var type = e.currentTarget.dataset.type;
    if (type == 1) {
			this.setData({
				pop1: 0,
			});
    } else if (type == 2) {
			this.setData({
				pop2: 0,
			});
    } else {
			wx.showToast({
				title: '弹窗错误', icon: 'none'
			}); return
    }
  },

  // 关闭弹窗
  popClose: function (e) {
    console.log(e);
    var type = e.currentTarget.dataset.type;
    if (type == 1) {
			this.setData({
				pop1: 1,
			});
    } else if (type == 2) {
			this.setData({
				pop2: 1,
				attr_id: 0,
				attr_key: '',
				attr_val: '',
			});
    } else {
        wx.showToast({
          title: '弹窗错误', icon: 'none'
        }); return
    }
  },

  // 弹窗属性
  attrEdit: function (e) {
		// console.log(e, this.data.attrList);
		var id = e.currentTarget.dataset.id;
		var attrOne = '', attrList = this.data.attrList;
		for (var i = 0; i < attrList.length; i ++) {
			if (id == attrList[i].id) {
				attrOne = attrList[i];
			}
		}
		console.log(attrOne);
		if (attrOne == '') {
			wx.showToast({
				title: '没有属性', icon: 'none',
			}); return
		}
		this.setData({
			attr_id: id,
			attr_key: attrOne.attr_key,
			attr_val: attrOne.attr_val,
			pop2: 0,
		});
  },

  // 记录
  toTitle: function (e) {
    this.setData({
        title: e.detail.value,
    });
  },

  // 记录
  toDescribe: function (e) {
    this.setData({
        describe: e.detail.value,
    });
  },

  // 记录
  toAttrKey: function (e) {
    this.setData({
        attr_key: e.detail.value,
    });
  },

  // 记录
  toAttrVal: function (e) {
    this.setData({
        attr_val: e.detail.value,
    });
  },

  // 更新物品名
  basicSave: function (e) {
    var that = this;
    var formData = {
        uid: this.data.uid,
        id: this.data.id,
        title: this.data.title,
        describe: this.data.describe,
    };
    wx.request({
        method: 'POST',
        url: app.globalData.domainXcx + '/tmp/modify',
        data: formData,
        timeout: 10000, //接口超时10S
        success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
                wx.showToast({
                    title: res.msg, icon: 'none'
                }); return
            }
            wx.showToast({
              title: '修改成功', icon: 'none'
            });
            that.viewReload();
        },
        fail: function () {
            console.log('error');
        }
    });
  },

  // 更新属性
  attrModify: function (e) {
    var that = this;
    // console.log(e);
    var formData = {
			id: this.data.id,
			attr_id: this.data.attr_id,
			attr_name: this.data.attr_key,
			attr_val: this.data.attr_val,
    };
    console.log(formData); //return
    wx.request({
			method: 'POST',
			url: app.globalData.domainXcx + '/tmp/attr/modify',
			data: formData,
			timeout: 10000, //接口超时10S
			success: function (res) {
				res = res.data;
				console.log(res);
				if (res.code < 0) {
					wx.showToast({
						title: res.msg, icon: 'none'
					}); return
				}
				wx.showToast({
					title: '更新成功', icon: 'none'
				});
				that.viewReload();
			},
			fail: function () {
				console.log('error');
			}
    });
	},
	
	// 属性删除弹窗
	attrDelPop: function (e) {
		var attr_id = e.currentTarget.dataset.id;
		wx.showModal({
			title: '提示',
			content: '确认要删除该属性吗？',
			complete: (res) => {
				if (res.cancel) {
					console.log('取消');
				} else if (res.confirm) {
					console.log('确认');
					this.attrDel(attr_id);
				}
			}
		})
	},

  // 属性删除
  attrDel: function (e) {
    var that = this;
    var formData = {
        attr_id: e,
    };
    console.log(formData); //return
    wx.request({
			method: 'POST',
			url: app.globalData.domainXcx + '/tmp/attr/del',
			data: formData,
			timeout: 10000, //接口超时10S
			success: function (res) {
				res = res.data;
				console.log(res);
				if (res.code!=0) {
					wx.showToast({
						title: res.msg, icon: 'none'
					}); return
				}
				wx.showToast({
					title: '删除成功', icon: 'none'
				});
				that.viewReload();
			},
			fail: function () {
				console.log('error');
			}
    });
  },

  // 刷新页面
  viewReload: function (e) {
    var param = '?code=' + this.data.code;
    wx.navigateTo({
        url: '/pages/index/tmpOne/index' + param,
    }); 
  },
})